# -*- coding:utf-8 -*-
import sys
import numpy as np
import os

if __name__ == "__main__":
    print 'Total:', len(sys.argv)
    print 'list:', str(sys.argv)

    path = os.getcwd()
    str_files = os.listdir(path)

    for name in str_files:
        print(name)
        str_name = name.split('.')
        if(len(str_name) != 2 or str_name[1] != "txt"):
            continue
        filename = name
        saved_name = str_name[0] + ".pcm"
        file = open(filename,"r")
        file_pcm = open(saved_name,"wb")
        data = file.read()
        data_array = data.split(' ')
        print(data_array[3])
        print("-------------------------")
        # dig_data = map(int,data_array[3])
        s_data = int(data_array[3],16)
        print (int(data_array[3],16))
        print (chr(s_data/256) + chr(s_data%256))
        for ssstr in data_array:
            if(len(ssstr) > 0):
                s_data = int(ssstr,16)
                file_pcm.write(chr(s_data / (256) ))
                file_pcm.write(chr(s_data % (256) ))

            

        # print(bytes(bytearray.fromhex(data_array[3])))
        print("-------------------------")
        file.close()
        file_pcm.close()
